package edu.mtu.citizenscience.mushroommapper.activity.main;

import java.util.Locale;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import edu.mtu.citizenscience.cs.sdk.login.CitsciLogin;
import edu.mtu.citizenscience.cs.sdk.login.CitsciLoginCallback;
import edu.mtu.citizenscience.cs.sdk.login.CitsciLoginError;
import edu.mtu.citizenscience.cs.sdk.login.UserLogin;
import edu.mtu.citizenscience.cs.utils.Message;
import edu.mtu.citizenscience.mushroommapper.MMActivity;
import edu.mtu.citizenscience.mushroommapper.activity.R;
import edu.mtu.citizenscience.mushroommapper.activity.main.settings.LoginDialog;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Settings Activity which allows the user to login to
 * and register for a citsci account
 */
public class SettingsActivity extends MMActivity {
	// citsci interface for logging the user in
	private CitsciLogin citsciLogin;
	// dialog to display while logging the user in
	private LoginDialog loginDialog = new LoginDialog();
	
	/**
	 * @since 1.0
	 * 
	 * Callbacks for handling the user login
	 */
	private class HandleLogin implements CitsciLoginCallback {

		/**
		 * Called on successful login
		 * 
		 * @param login - UserLogin - the user's username and password
		 * @return void
		 */
		public void loginSuccess(UserLogin login)
		{
			// dismiss the login dialog as we now have the login
			loginDialog.dismiss();
			
			// update the login display
			updateLoginDisplay();
		}

		/**
		 * Called on login error
		 * 
		 * @param error - CitsciLoginError - enum for specific error
		 * @return void
		 */
		public void loginError(CitsciLoginError error) 
		{
			// did not get the login so dismiss the dialog
			loginDialog.dismiss();
			
			// switch over the error types and display the appropriate message to the user
			switch (error)
			{
				case NONE: break;
				case INVALID_RESPONSE:
					Message.showMessage(aq.getContext(), "Error: Invalid Response");
				break;
				case NO_CONNECTION:
					Message.showMessage(aq.getContext(), "Could not connect to sever");
				break;
			}
			
		}

		/**
		 * Called on error message from the login
		 * 
		 * @param message - String - The error message
		 * @return void
		 */
		public void loginErrorMessage(String message) 
		{
			// did not get the login so dismiss the dialog{
			loginDialog.dismiss();
			
			// display the error message to the user
			Message.showMessage(aq.getContext(), message);
		}
	}

	/**
	 * Settings Activity creation handler
	 * 
	 * @param   savedInstanceState - Bundle - Bundle with launch info
	 * @return	void
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        this.setContentView(R.layout.settings_activity);
        
        // create new login object
        this.citsciLogin = new CitsciLogin(this.aq);
        
        // set on click handler for the register button
        this.aq.id(R.id.register_button).clicked(this, "register");
        
        // update the login display
        this.updateLoginDisplay();
    }
	
	/**
	 * Updates the login display for if the user is logged in or not
	 * 
	 * @param   void
	 * @return	void
	 */
	public void updateLoginDisplay()
	{
		// get the current user login
		UserLogin login = this.citsciLogin.getLogin();
		
		// check to see if we have the users login
		if (login.haveLogin())
        {
			// set the login logout button text to logout and set the clicked handler as logoutUser
        	this.aq.id(R.id.login_logout_button).text(R.string.logout_label_button).clicked(this, "logoutUser");
        	
        	// set the username input the users username and disable it from editing
        	this.aq.id(R.id.user_input).text(login.getUsername()).enabled(false);
        	// hide the register button
        	this.aq.id(R.id.register_button).invisible();
        	// fill the password field with fake_pass and disable it from editing
        	this.aq.id(R.id.password_input).text("fake_passs").enabled(false);
        }
        else
        {
        	// set the login logout button text to login and set the clicked handler as loginUser
        	this.aq.id(R.id.login_logout_button).text(R.string.login_label_button).clicked(this, "loginUser");
        	// clear the user input field and enable it
        	this.aq.id(R.id.user_input).text("").enabled(true);
        	// display the register button
        	this.aq.id(R.id.register_button).visible();
        	// clear the password input field and enable it
        	this.aq.id(R.id.password_input).text("").enabled(true);
        }
	}
	
	/**
	 * onClick handler for the login button
	 * 
	 * @param   void
	 * @return	void
	 */
	public void loginUser()
	{
		// get the username from the user input and set it to lower case
		String username = this.aq.id(R.id.user_input).getText().toString().toLowerCase(Locale.US);
		// get the password from the password input
		String password = this.aq.id(R.id.password_input).getText().toString();
		
		// display the login dialog so the user knows the app is working
		this.loginDialog.show(getSupportFragmentManager(), "loginDialog");
		
		// do login of the user with the username, password and handle login callbacks
		this.citsciLogin.doLogin(username, password, new HandleLogin());
	}
	
	/**
	 * onClick handler for the logout button
	 * 
	 * @param   void
	 * @return	void
	 */
	public void logoutUser()
	{
		// clear the stored login
		this.citsciLogin.clearLogin();
		
		// update the login display
		this.updateLoginDisplay();
	}
	
	/**
	 * onClick handler for the register button
	 * 
	 * @param   void
	 * @return	void
	 */
	public void register()
	{
		// launch an intent to view the register page on the website in the web browser
		this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://citizenscience.cs.mtu.edu/portal/mushroom/page/register")));
	}
}
